package com.zp.base.admin.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zp.base.admin.common.aspect.AddOperatorLog;
import com.zp.base.admin.common.constant.BizCodeEnum;
import com.zp.base.admin.common.utils.PageUtils;
import com.zp.base.admin.common.utils.R;
import com.zp.base.admin.common.utils.StringAndDateUtil;
import com.zp.base.admin.config.SiteConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.zp.base.admin.entity.DatabaseBakEntity;
import com.zp.base.admin.service.DatabaseBakService;



/**
 * 
 *
 * @author zhangpeng
 * @email zhangpeng@aliyun.com
 * @date 2021-02-28 11:15:56
 */
@Controller
@Slf4j
@RequestMapping("admin/database_bak")
public class DatabaseBakController {
    @Autowired
    private DatabaseBakService databaseBakService;
    @Autowired
    private SiteConfig siteConfig;


    /**
     * 列表
     */
    @GetMapping("/list")
    @AddOperatorLog(content = "查看备份数据列表")
    @PreAuthorize("hasAuthority('database_bak:list')")
    public String list(@RequestParam(name = "currentPage", defaultValue = "1") String page, Model model){

        HashMap<String, Object> map = new HashMap<>();
        map.put("page", page);
        PageUtils pageUtils = databaseBakService.queryPage(map);

        model.addAttribute("pageBean", pageUtils);
        model.addAttribute("title","备份数据列表");
        model.addAttribute("siteName", siteConfig.getSiteName());
        return "admin/database_bak/list";
    }

    /**
     * 数据库数据备份
     */
    @PostMapping("/add")
    @AddOperatorLog(content = "数据库数据备份")
    @ResponseBody
    @PreAuthorize("hasAuthority('database_bak:add')")
    public R save(){
        try {
            boolean save = databaseBakService.saveDatabaseBakEntity();
            if (!save) {
                return R.error(BizCodeEnum.DATABASE_BACKUP_ERROR.getCode(), BizCodeEnum.DATABASE_BACKUP_ERROR.getMsg());
            }
            log.info("数据库信息备份成功");
            return R.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(BizCodeEnum.DATABASE_BACKUP_ERROR.getCode(), BizCodeEnum.DATABASE_BACKUP_ERROR.getMsg());
        }
    }


    /**
     * 删除数据库备份信息及文件
     */
    @PostMapping("/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('database_bak:delete')")
    @AddOperatorLog(content = "删除数据库备份信息及文件")
    public R delete(@RequestParam(name = "ids") String ids) {
        if (ids == null || ids.length() <= 0) {
            return R.error(BizCodeEnum.DELETE_DATABASE_BACKUP_NO_IDS.getCode(), BizCodeEnum.DELETE_DATABASE_BACKUP_NO_IDS.getMsg());
        }
        String[] split = ids.split(",");
        List<Long> collect = Arrays.stream(split).map(s -> Long.valueOf(s)).collect(Collectors.toList());
        //删除数据库备份信息及文件
        boolean b = databaseBakService.deleteByIds(collect);
        if (!b) {
            return R.error(BizCodeEnum.DELETE_DATABASE_BACKUP_ERROR.getCode(), BizCodeEnum.DELETE_DATABASE_BACKUP_ERROR.getMsg());
        }
        return R.ok();
    }


    /**
     * 数据库数据还原
     */
    @PostMapping("/restore")
    @AddOperatorLog(content = "数据库数据还原")
    @ResponseBody
    @PreAuthorize("hasAuthority('database_bak:restore')")
    public R restore(@RequestParam("id") Long id){
        try {
            boolean restore = databaseBakService.restoreById(id);
            if (!restore) {
                return R.error(BizCodeEnum.DATABASE_RESTORE_ERROR.getCode(), BizCodeEnum.DATABASE_RESTORE_ERROR.getMsg());
            }
            log.info("数据库还原成功！");
            return R.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(BizCodeEnum.DATABASE_RESTORE_ERROR.getCode(), BizCodeEnum.DATABASE_RESTORE_ERROR.getMsg());
        }
    }

}
